package com.example.flashsales.controller;


import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.vo.OrdersVo;
import com.example.flashsales.entity.vo.ReturnOrderVo;
import com.example.flashsales.service.impl.OrderServiceImpl;
import com.example.flashsales.service.impl.RefundServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderServiceImpl orderService;

    @Autowired
    RefundServiceImpl refundService;


    /**
     * 根据订单id获取订单
     * @param orderId
     * @return
     */
    @ResponseBody
    @GetMapping("/viewOrderById")
    public Result viewOrderById(Long orderId){
        if (orderId == null){
            return Result.error("订单id不能为空");
        }
        final List<OrdersVo> ordersVoList = orderService.viewOrder(orderId);
        if (ordersVoList.isEmpty()){
            return Result.ok("查看成功");
        }
        return Result.ok("查看成功").setData(ordersVoList.get(0).getOrderDetailsVoList());
    }

    /**
     * 根据退货订单id获取订单
     * @param refundId
     * @return
     */
    @ResponseBody
    @GetMapping("/viewRefundById")
    public Result viewRefundById(Long refundId){
        if (refundId == null){
            return Result.error("订单id不能为空");
        }
        final ReturnOrderVo returnOrderVo = refundService.viewRefundDetails(refundId);
        return Result.ok("查看成功").setData(returnOrderVo.getReturnOrderDetailsVos());
    }



}
